package org.icy.modules.router;

import java.net.URLClassLoader;

import org.ice.exception.IceException;
import org.ice.http.HttpRequest;
import org.ice.module.IModule;
import org.ice.module.router.IRouter;
import org.ice.utils.StringUtils;
import org.icy.core.loaders.ClassLoaderFactory;

public class ExternalModuleRouter implements IRouter {

	@Override
	public IModule route(HttpRequest request) throws IceException {
		String moduleName = request.getModuleName();
		moduleName = new StringBuilder("module.").append(StringUtils.ucwords(moduleName, '-', true).replaceAll("-", "")).append("Module").toString();
		try {
			ClassLoader cl = ClassLoaderFactory.getClassLoader("modules");
			Class<?> c = Class.forName(moduleName, false, cl);
			Object obj = c.newInstance();
			if (obj instanceof IModule)
				return (IModule) obj;
		} catch (Exception ex)	{
			ex.printStackTrace();
		}
		return null;
	}

}
